package com.yc.common.utils;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

/**
 * @author zhupanlin
 * @version 1.0
 * @description: TODO
 * @date 2023/8/2 20:12
 */
public class LocationUtils {

    /**
     * 获取指定IP对应的经纬度（为空返回当前机器经纬度）
     * @return
     */
    public static String[] getIPXY(HttpServletRequest request) {

        //获取本机公网ip
        /*String ip = getIpAddress(request);
        System.out.println("ip = " + ip);

        if(ip==null || ip.trim().equals("")) return null;*/

        // 高德地图 api key
        String ak = "33a04088def54d95884befe3dd4593df";

        try {
            URL url = new URL("https://restapi.amap.com/v3/ip?output=xml&key=" + ak);
            InputStream inputStream = url.openStream();
            InputStreamReader inputReader = new InputStreamReader(inputStream);
            BufferedReader reader = new BufferedReader(inputReader);
            StringBuilder sb = new StringBuilder();
            String str;

            do {
                str = reader.readLine();
                sb.append(str);
            } while (null != str);


            str = sb.toString();


            if (str.isEmpty()) {
                return null;
            }


            // 获取坐标位置
            int index = str.indexOf("<rectangle>");
            String temp = str.substring(index);
            temp = temp.replaceAll("[<]*[/]*[a-zA-Z]*[>]*", "");
            String[] location = temp.split(";");

            return location;


        } catch (IOException e) {
            e.printStackTrace();
        }


        return null;
    }


        /**
         * 获取IP地址
         */
        public static String getIpAddress(HttpServletRequest request) {
            String ip = request.getHeader("x-forwarded-for");
            if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip) && ip.contains(",")) {
                ip = ip.split(",")[0];
            }

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("X-Real-IP");
            }

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }

            if ("0:0:0:0:0:0:0:1".equals(ip)) {
                ip = "127.0.0.1";
            }

            return ip;
        }

}
